There are two ways to create a full-text index:
Using T-SQL Commands to Build Full-Text Indexes and Catalogs
In SQL 2008 full-text catalogs are
“virtual.” They are just containers for full-text catalog properties
like accent sensitivity or catalog rebuild or population statements.
They live inside the database in SQL 2008, unlike SQL 2000 and 2005,
where the catalogs and full-text indexes resided in the filesystem.
To build your full-text catalogs and indexes, you need to use the CREATE FULLTEXT commands.
Note
T-SQL commands are not case sensitive.
There are three commands for full-text index creation and maintenance:
Let’s look at how they work.
CREATE FULLTEXT CATALOG
To create a full-text catalog in its simplest form, you enter this command:
USE AdventureWorks;
Create fulltext catalog MyCatalog
In this command, MyCatalog is the name of the catalog. The CREATE FULLTEXT CATALOG statement has several switches:
We next cover each of these parameters.
ON FILEGROUP
The ON FILEGROUP command is for backward compatibility only.
IN PATH
The IN PATH command is for backwards compatibility only.
WITH ACCENT_SENSITIVITY
The WITH ACCENT_SENSITIVITY option allows you to create a catalog that is sensitive (Accent_Sensitivity ON) to accents (the default) or insensitive to accents. With Accent_Sensitivity OFF, a search on café would match with café and cafe. Likewise, a search on cafe would match with cafe and café. With Accent_Sensitivity ON, a search on café would match only with café and not cafe. Likewise, a search on cafe would match only with cafe and not café.
The following is the typical syntax for using the option:
USE AdventureWorks;
CREATE FULLTEXT CATALOG MyCatalog ON FILEGROUP MyFileGroup
WITH ACCENT_SENSITIVITY = OFF
AS DEFAULT
The AS DEFAULT
option allows you to create a default full-text catalog for every
full-text index in a database. This option is convenient because you
don’t have to specify the full-text catalog for your full-text indexes.
Ideally, each large table will have its own full-text catalog, so
although it saves some time typing the commands, this option is not the
best one to use all the time. For example, when you create a full-text
index, at a minimum, you need to use the following:
Use AdventureWorks;
CREATE FULLTEXT INDEX ON person.Contact(Firstname)
KEY INDEX pk_Contact_ContactID ON MyCatalog
With a default catalog for your database, all you have to type is this:
Use AdventureWorks;
CREATE FULLTEXT INDEX ON person.Contact(Firstname) KEY INDEX pk_Contact_ContactID
If you do not have a default catalog, you get the following error message:
Msg 9967, Level 16, State 1, Line 2
A default full-text catalog does not exist in database 'AdventureWorks'
or user does not have permission to perform this action.
Here is the syntax to create a default catalog: Create FullText Catalog AdventureWorksFT as Default
Note
You can have only one default catalog per database.
AUTHORIZATION
The AUTHORIZATION
option allows a user or a role to own and consequently manage a
full-text catalog. The following is the typical syntax for using this
option:
Use AdventureWorks;
CREATE FULLTEXT CATALOG MyCatalog WITH ACCENT_SENSITIVITY =OFF Authorization [dbo]
Now that you know how to create a catalog, let’s create full-text indexes on the tables that will be stored in these catalogs.
CREATE FULLTEXT INDEX
You use the CREATE FULLTEXT INDEX command to create full-text indexes. Your searches query full-text indexes to return results.
The CREATE FULLTEXT INDEX command has several parameters:
We next cover each of these parameters.
COLUMN NAME
The COLUMN NAME parameter is the char, varchar, nchar, nvarchar, text, or xml column that you are full-text indexing. A minimal CREATE FULLTEXT INDEX statement would look like this:
Use AdventureWorks;
CREATE FULLTEXT INDEX ON Person.Contact(Firstname) KEY INDEX pk_Contact_ContactID
In this example, Person.Contact is the name of the table you are full-text indexing, and pk_Contact_ContactID
is the Full-Text Search key. A Full-Text Search key must be a unique,
non-nullable, single-column index that is not offline and has a maximum
size of 900 bytes. Note that this full-text index is created on the
default full-text catalog. Once again, if you do not have a default
full-text catalog, you get the following message:
A default full-text catalog does not exist in database 'AdventureWorks'
or user does not have permission to perform this action.
You specify a full-text catalog by using the following command:
Use AdventureWorks;
CREATE FULLTEXT INDEX ON Person.Contact(Firstname, Lastname)
KEY INDEX pk_Contact_ContactID ON MyCatalog
In this command, MyCatalog is the name of the full-text catalog. Note that the second example creates a full-text index on two columns.
TYPE COLUMN
You can index columns of the char, varchar, nchar, nvarchar, text, ntext, and xml data types. You can also index columns of the image and varbinary(max)
data types if you have an ancillary column, which tells what the
content is in that column. The reason for this is the Indexer needs to
interpret the binary data in the image and varbinary(max)
columns and will need to load an IFilter, which corresponds to the
binary data stored in these columns. The Indexer will load the IFilter,
which corresponds to the extension stored in this ancillary column. For
example, if you store a PDF in the image or varbinary(max) column, the ancillary column would have to have the value PDF or .PDF (not case sensitive) so the PDF IFilter would be launched.
There is a relevant example in the AdventureWorks database. Consider the Production.Document table. If you want to search Word documents stored in this table, a LIKE clause search would not work because the Word documents are in binary format. Full-text indexing on the Document
column similarly wouldn’t work because the IFilter would not be able to
interpret the binary stream. So you need to add a column that contains
the extension the document in this row would have if it were stored in
the filesystem; in this case, it’s a Word document, so the extension
would be .doc. This column is called a document type column. In the Production.Document table, this column is the FileExtension column. When you index binary large objects (BLOBs), there are special considerations, as discussed in the next section.